<template>
  <div>
    <el-dialog width="50%" :title="dialogTitle" @close="closeDialog" :visible.sync="dialogFormVisible" :destroy-on-close="true" :append-to-body="true" :close-on-click-modal="false">
      <el-form :model="sysOrgForm" :inline="true" :rules="sysOrgRules" ref="sysOrgForm">
        <el-form-item label="上级组织" :label-width="formLabelWidth" prop="parentId">
          <el-input v-model="sysOrgForm.parentName" autocomplete="off" @click.native="openTreeDialog"></el-input>
        </el-form-item>
        <el-form-item label="组织名称" :label-width="formLabelWidth" prop="orgName">
          <el-input v-model="sysOrgForm.orgName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="负责人姓名" :label-width="formLabelWidth" prop="leaderUserName">
          <el-input v-model="sysOrgForm.leaderUserName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="负责人电话" :label-width="formLabelWidth" prop="leaderPhone">
          <el-input v-model="sysOrgForm.leaderPhone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序" :label-width="formLabelWidth" prop="orderNum">
          <el-input type="number" v-model="sysOrgForm.orderNum" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="组织类型" :label-width="formLabelWidth" prop="orgType">
          <el-radio-group v-model="sysOrgForm.orgType">
            <el-radio label="0">组织</el-radio>
            <el-radio label="1">部门</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="组织状态" :label-width="formLabelWidth" prop="status">
          <el-radio-group v-model="sysOrgForm.status">
            <el-radio label="0">启用</el-radio>
            <el-radio label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="addOrEditSysOrg">确 定</el-button>
      </div>
      <OrgTreeDialog/>
    </el-dialog>
  </div>
</template>

<script>
  import {Message} from "element-ui";
  import {addSysOrg, editSysOrg, getSysOrgs} from "@/api/sysOrg";
  import OrgTreeDialog from "@/components/dialog/OrgTreeDialog";
  export default {
    name: "SysOrgDialog",
    components: {
      OrgTreeDialog,
    },
    data() {
      return {
        formLabelWidth: '120px',
          sysOrgRules: {
              orgName: [
                { required: true, message: '请输入组织名称', trigger: 'blur' }
              ],
              parentName: [
                { required: true, message: '请选择上级组织', trigger: 'blur' }
              ],
              status: [
                { required: true, message: '请选择组织状态', trigger: 'blur' }
              ],
              orgType: [
                { required: true, message: '请选择组织类型', trigger: 'blur' }
              ]
        }
      }
    },
    computed: {
      dialogFormVisible: {
        get() {
          return this.$store.state.org.dialogFormVisible;
        },
        set(value) {
          this.$store.state.org.dialogFormVisible = value;
        }
      },
      dialogTitle: {
        get() {
          return this.$store.state.org.dialogTitle;
        },
        set(value) {
          this.$store.state.org.dialogTitle = value;
        }

      },
      sysOrgForm: {
        get() {
          return this.$store.state.org.sysOrgForm;
        },
        set(value) {
          this.$store.state.org.sysOrgForm = value;
        }
      }
    },
    methods: {
      openTreeDialog() {
        this.$store.dispatch("org/openTreeDialog", true);
      },
      refreshData() {
        var params = {
          "current": this.$store.state.org.currentPage,
          "size": this.$store.state.org.pageSize
        };
        // 刷新
        getSysOrgs(params).then((res) => {
          this.$store.state.org.tableData = res.data.data;
          this.$store.state.org.total = res.data.total;
          this.clearForm();
        })
      },
      clearForm() {
        Object.keys(this.sysOrgForm).forEach((key) => {
          this.sysOrgForm[key] = null;
        })
        this.$store.state.org.sysOrgForm.parentName = "顶级组织";
        this.$store.state.org.sysOrgForm.parentId = "0";
      },
      closeDialog() {
        this.$store.state.org.dialogFormVisible = false;
        this.clearForm();
      },
      addOrEditSysOrg() {
        this.$refs["sysOrgForm"].validate((valid) => {
          if (valid) {
            if (this.dialogTitle === "添加") {
              addSysOrg(this.sysOrgForm).then((res) => {
                Message.success(res.data.msg);
                this.dialogFormVisible = false;
                this.refreshData();
              })
            } else {
              editSysOrg(this.sysOrgForm).then((res) => {
                Message.success(res.data.msg);
                this.dialogFormVisible = false;
                this.refreshData();
              })
            }
        } else {
          console.log('error submit!!');
          return false;
        }
        });
      }
    }
  }
</script>

<style scoped>

</style>